using System;
using System.Collections.Generic;
using Cinemachine;
using Gameplay.PVE.Utils;
using UnityEditor;
using UnityEngine;
using Yoozoo.Managers;
using Random = UnityEngine.Random;

namespace Gameplay.PVE.Editor
{
    public class RpgShootEditorHelper : MonoBehaviour
    {
        void Start()
        {
            Instance = this;
        }
        
        void Update()
        {
            RpgShootEditorManager.Instance.Update();
        }

        private void OnApplicationQuit()
        {
            Instance = null;
        }

        public static RpgShootEditorHelper Instance;
    }
        
    
    public class RpgShootEditorManager
    {
        public static RpgShootEditorManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new RpgShootEditorManager();
                }
                return instance;
            }
        }

        private static RpgShootEditorManager instance;

        private List<GameObject> bullets = new List<GameObject>();

        private RpgEditorUnit player;

        private RpgEditorUnit enemy;

        private GameObject wall;

        private GameObject car;

        private GameObject root;


        public void Init()
        {
            if (RpgShootEditorHelper.Instance == null)
            {
                root = new GameObject("Root");
                root.AddComponent<RpgShootEditorHelper>();
            }
        }

        public void Refresh()
        {
            if (root != null)
            {
                GameObject.DestroyImmediate(root);
                root = null;
            }

            RpgShootEditorHelper.Instance = null;
            Init();
            player?.Dispose();
            player = null;
            ClearTarget();
        }

        private void ClearTarget()
        {
            if (enemy != null)
            {
                enemy.Dispose();
                enemy = null;
            }

            if (wall != null)
            {
                GameObject.DestroyImmediate(wall);
                wall = null;
            }

            if (car != null)
            {
                GameObject.DestroyImmediate(car);
                car = null;
            }
        }
        
        public void Update()
        {
            player?.Update();
            enemy?.Update();
        }

        public void CreateWall()
        {
            ClearTarget();
            wall = GameObject.Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>(PveResourceManager.GetHeroPath(103) + ".prefab"));
            wall.transform.position = new Vector3(7, 0, 0);
        }

        public void CreateCar()
        {
            ClearTarget();
            car = GameObject.Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>(PveResourceManager.GetHeroPath(106) + ".prefab"));
            car.transform.position = new Vector3(7, 0, 0);
        }
        
        public void CreateEnemy()
        {
            ClearTarget();
            enemy = new RpgEditorUnit();
            enemy.Init(10);
            enemy.SetPosition(new Vector3(7,0,0));
            enemy.SetForward(Vector3.left);
        }

        public void CreateMy()
        {
            if (player != null)
            {
                return;
            }
            player = new RpgEditorUnit();
            player.Init(0);
            player.SetPosition(new Vector3(-7,0,0));
            player.SetForward(Vector3.right);
        }

        public bool IsShootGround
        {
            set
            {
                if (player != null)
                {
                    player.IsShootGround = value;
                }
            }
            get
            {
                if (player != null)
                {
                    return player.IsShootGround;
                }

                return false;
            }
        }

        public void SetParam(float speed,float duration,int count)
        {
            player?.SetBulletParam(speed,duration,count);
        }
        
        public void ShootHG()
        {
            player?.StartHG();
        }
        
        public void ShootSMG()
        {
            player?.StartSMG();
        }
        
        public void ShootMG()
        {
            player?.StartMG();
        }

        public void ShootAR()
        {
            player?.StartAR();
        }
        
        public void ShootSG()
        {
            player?.StartSG();
        }
        
        public void ShootSR()
        {
            player?.StartSR();
        }

        public Vector3 GetEnemyHitPoint(bool isShootGround)
        {
            var targetPosition = new Vector3(7,0,0);
            if (enemy != null)
            {
                targetPosition = enemy.GetHitPoint();
            }

            if (car != null)
            {
                targetPosition = car.transform.position + new Vector3(-0.9f,Random.Range(0,1.5f),Random.Range(-2.1f,2.1f));
            }

            if (wall != null)
            {
                targetPosition = wall.transform.position + new Vector3(-0.3f,Random.Range(0,0.75f),Random.Range(-0.3f,0.3f));
            }

            if (isShootGround)
            {
                targetPosition.y = 0;
                targetPosition += PveUtils.GetRandomVector3(new Vector3(-2,0,-2),new Vector3(2,0,2));
            }
            return targetPosition;
        }

        public int GetHitId()
        {
            if (enemy != null)
            {
                return (int) ESkillEffect.hitRole;
            }
            if (car != null)
            {
                return (int) ESkillEffect.hitObstacleCar;
            }
            if (wall != null)
            {
                return (int) ESkillEffect.hitObstacleBlock;
            }

            return 0;
        }
    }
}